#include "mainwindow.h"
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QMenu>
#include <QMenuBar>
#include <QToolBar>
#include <QAction>
#include <QCloseEvent>
#include "itemtypes.hpp"
#include <QGraphicsTextItem>
#include "graphicstextitem.h"
#include "graphicsrectitem.h"
#include "graphicsview.h"
#include "graphicsscene.h"
#include "additems.h"
#include <QDebug>
#include "times.h"
#include <QThread>
#include "actions.h"

MainWindow *gMainWindow = NULL;
MainWindow::MainWindow(QWidget *parent)
	: QMainWindow(parent)
{
	gMainWindow = this;

	this->setUpdatesEnabled(true);

	this->createSceneAndView();
	this->createAction();
	this->createMenuAndToolbar();
	this->createConnection();
}

MainWindow::~MainWindow()
{

}

// --- initialination; -------------------------------
void MainWindow::createSceneAndView()
{
	mview = new GraphicsView;
	mscene = new GraphicsScene(this);
	mscene->setSceneRect(0,0,1024,768);
	mview->setScene(mscene);
	this->setCentralWidget(mview);
}
void MainWindow::createAction()
{
	mactionQuit = new QAction(tr("Quit..."),this);
	mactionQuit->setIcon(QIcon(":/MindFirst/images/btn_win_close_off.gif"));

	mactionAdditem = new QAction(tr("AddText"),this);
	//mactionAdditem->setData(TestItemType);
	mactionAdditem->setData(tr("addtextitem"));
	maddRectitemac = new QAction(tr("AddRectItem"), this);
	maddRectitemac->setData(tr("addrectitem"));
	
}
void MainWindow::createMenuAndToolbar()
{
	QMenu * m = this->menuBar()->addMenu(tr("File"));
	//filemu->insertAction(mactionNew);
	m->addAction(gActions->mNew);
	m->addSeparator();
	m->addAction(mactionQuit);

	m = this->menuBar()->addMenu(tr("Help"));
	m->addAction(gActions->mHelp);

	QToolBar * t = this->addToolBar(tr("File"));
	t->addAction(gActions->mNew);
	t->addAction(mactionQuit);

	t->addSeparator();
	t->addAction(mactionAdditem);
	t->addAction(maddRectitemac);

	t->addSeparator();
	t->addAction(gActions->mSelectTool);
	t->addAction(gActions->mRectTool);
	t->addAction(gActions->mLineTool);

	t->addSeparator();
	t->addAction(gActions->mHelp);
}
void MainWindow::createConnection()
{
	connect(gActions->mNew, SIGNAL(triggered()), this, SLOT(newFile()));
	connect(mactionQuit,SIGNAL(triggered()),this, SLOT(close()));
	connect(mactionAdditem,SIGNAL(triggered()),gAddItems,SLOT(addItemEvent()));
	connect(maddRectitemac,SIGNAL(triggered()),gAddItems,SLOT(addItemEvent()));
	
}

// --- common methods; --------------------------
void MainWindow::setDirty(bool on)
{
	this->setWindowModified(on);
	//this->update();
}
void MainWindow::connectItem(QObject * item)
{
	//connect(item, SIGNAL(dirty()), this, SLOT(setDirty()));
}
// --- actions; ---------------------------------
void MainWindow::newFile()
{
	int i=0;
	i=1;
}

// --- overwrite methods; ------------------------

void MainWindow::closeEvent(QCloseEvent * event)
{
	event->accept();
}
void MainWindow::mousePressEvent(QMouseEvent * event)
{
	Times::outCurrentTime("mainWindow");
	qDebug() << "->MainWindow.mousePressEvent() enter.";
	QThread::usleep(1);

	QMainWindow::mousePressEvent(event);
}